package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.entity.MenuVo;
import com.neusoft.entity.Rent;
import com.neusoft.entity.Room;
import com.neusoft.entity.SystemMenu;
import com.neusoft.entity.common.CommonResult;
import com.neusoft.service.SystemMenuService;
import com.neusoft.util.TreeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * 系统菜单表(SystemMenu)表控制层
 *
 * @author makejava
 * @since 2020-05-27 16:58:52
 */
@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    /**
     * 服务对象
     */
    @Resource
    private SystemMenuService systemMenuService;

    @GetMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id){
        System.out.println("查询单条数据"+id);
        SystemMenu bean = systemMenuService.queryById(id);
        System.out.println("bean对象为："+bean);
        return bean;
    }

    @GetMapping("queryAll1")
    @ResponseBody
    public List<SystemMenu> queryAll1(){
        return systemMenuService.queryAll1();
    }

    @PostMapping("deleteById/{ids}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        System.out.println(ids);
        return systemMenuService.deleteById(Arrays.asList(ids));
    }

    @PostMapping("deleteById1/{id}")
    @ResponseBody
    public boolean deleteById1(@PathVariable Integer id) {
        if ( id == null) {
            return false;
        }
        System.out.println(id);
        return systemMenuService.deleteById1(id);
    }

    @PutMapping("save")
    @ResponseBody
    public Object save(@RequestBody SystemMenu bean) {
        System.out.println("修改的菜单对象为："+bean);
        boolean result = false;
        //判断账号是否存在
        //判断是添加还是修改
        if (bean.getId() != null) {
            result = systemMenuService.update(bean);
        } else {
            result = systemMenuService.insert(bean).getId() != null;
        }
        return result;
    }

//    @PutMapping("updateAll")
//    @ResponseBody
//    public boolean updateAll(List<SystemMenu> systemMenuList){
//        for(SystemMenu systemMenu:systemMenuList){
//            systemMenu.setStatus(0);
//            save(systemMenu);
//        }
//        return true;
//    }

    @GetMapping("menus")
    @ResponseBody
    public Map<String,Object> menus() {
        Map<String, Object> map = new HashMap<>(16);
        Map<String,Object> home = new HashMap<>(16);
        Map<String,Object> logo = new HashMap<>(16);
        List<SystemMenu> menuList = systemMenuService.queryAll();
        List<MenuVo> menuInfo = new ArrayList<>();
        for (SystemMenu e : menuList) {
            MenuVo menuVO = new MenuVo();
            menuVO.setId(e.getId());
            menuVO.setPid(e.getPid());
            menuVO.setHref(e.getHref());
            menuVO.setTitle(e.getTitle());
            menuVO.setIcon(e.getIcon());
            menuVO.setTarget(e.getTarget());
            menuInfo.add(menuVO);
        }
        map.put("menuInfo", TreeUtil.toTree(menuInfo, 0));
        home.put("title","首页");
        home.put("href","/page/welcome-1");//控制器路由,自行定义
        logo.put("title","后台管理系统");
        logo.put("image","/static/images/back.jpg");//静态资源文件路径,可使用默认的logo.png
        map.put("homeInfo", "{title: '首页',href: '/ruge-web-admin/page/welcome.html'}}");
        map.put("logoInfo", "{title: 'RUGE ADMIN',image: 'images/logo.png'}");
        return map;
    }

    @GetMapping("getAuths")
    @ResponseBody
    public Object getAuths() {
        CommonResult<SystemMenu> result = new CommonResult<>();
        List<SystemMenu> list = systemMenuService.queryAll();
        result.setCode(0);
        result.setCount((long) list.size());
        result.setData(list);
        return result;
    }

    @PostMapping("queryAll/{page}/{limit}")
    @ResponseBody
    public Object queryAll(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody SystemMenu bean) {
        System.out.println("查询全部菜单数据："+bean);
        CommonResult<SystemMenu> result = new CommonResult<>();
        IPage<SystemMenu> ipage = systemMenuService.queryAllByLimit(page, limit, bean);
        result.setCode(0);
        result.setCount(ipage.getTotal());
        result.setData(ipage.getRecords());
        return result;
    }



}
